package com.ruoyi.system.service.impl;
import com.ruoyi.system.entity.*;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.service.SysMonitorService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 报警信息表(SysMonitor)表服务实现类
 * @since 2022-11-17 09:40:56
 */
@Service("sysMonitorService")
public class SysMonitorServiceImpl implements SysMonitorService {
    @Resource
    private SysMonitorMapper sysMonitorDao;

    @Resource
    private SysDangerPointMapper sysDangerPointMapper;

    @Resource
    private SysGasPointMapper sysGasPointMapper;

    @Resource
    private SysFirePointMapper sysFirePointMapper;

    @Resource
    private SysEnvironmentPointMapper sysEnvironmentPointMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SysMonitor queryById(Integer id) {
        return this.sysMonitorDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param sysMonitor 筛选条件
     * @return 查询结果
     */
    @Override
    public List<SysMonitor> queryByPage(SysMonitor sysMonitor) {
        return sysMonitorDao.queryAllByLimit(sysMonitor);
    }

    /**
     * 新增数据
     *
     * @param sysMonitor 实例对象
     * @return 实例对象
     */
    @Override
    public SysMonitor insert(SysMonitor sysMonitor) {
        this.sysMonitorDao.insert(sysMonitor);
        return sysMonitor;
    }

    /**
     * 修改数据
     *
     * @param sysMonitor 实例对象
     * @return 实例对象
     */
    @Override
    public SysMonitor update(SysMonitor sysMonitor) {
        this.sysMonitorDao.update(sysMonitor);
        return this.queryById(sysMonitor.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.sysMonitorDao.deleteById(id) > 0;
    }

    @Override
    public List<SysMonitor> queryByDataAddress() {
        return sysMonitorDao.queryByDataAddress();
    }

    @Override
    public Map<String, Object> getAlertCount(String startTime, String endTime) {
        Map<String,Object> result = new HashMap<>();
        // 重大危险源报警统计
        Map<String, Object> dangerMap = new HashMap<>();
        List<Map<String, Integer>> danger = sysMonitorDao.getAlertCount("重大危险源",startTime,endTime);
        dangerMap.put("byLevel",danger);
        result.put("danger",dangerMap);
        Map<String, Object> gasMap = new HashMap<>();
        List<Map<String, Integer>> gas = sysMonitorDao.getAlertCount("可燃气体",startTime,endTime);
        gasMap.put("byLevel",gas);
        result.put("gas",gasMap);
        int count = (int)sysMonitorDao.count(new SysMonitor());
        result.put("total",count);
        // 可燃有毒气体统计
        return result;
    }

    @Override
    public List<Map<String, Object>> countByType() {
        return sysMonitorDao.countByType();
    }

    @Override
    public SysMonitor queryByDataBase(String databaseBitNumber) {
        return sysMonitorDao.queryByDataBase(databaseBitNumber);
    }

    @Override
    public List<SysMonitor> countByState() {
        return sysMonitorDao.countByState();
    }

    @Override
    public Map<String, Object> countAll() {
        Map<String, Object> map = new HashMap<>();
        long count = sysDangerPointMapper.count(new SysDangerPoint());
        long count3 = sysGasPointMapper.count(new SysGasPoint());
        long total = count + count3;
        map.put("total",total);
        SysMonitor sysMonitor = new SysMonitor();
        sysMonitor.setMonitorState("未恢复");
        List<SysMonitor> monitors = sysMonitorDao.queryAllByLimit(sysMonitor);
        SysMonitor sysMonitor1 = new SysMonitor();
        sysMonitor1.setMonitorState("已超时");
        List<SysMonitor> monitors1 = sysMonitorDao.queryAllByLimit(sysMonitor1);
        int size1 = monitors1.size();
        int size = monitors.size();
        map.put("normal",total - size - size1);
        map.put("anomaly",size + size1);
        return map;
    }

    @Override
    public List<SysMonitor> countAllAnomaly() {
        SysMonitor sysMonitor = new SysMonitor();
        sysMonitor.setMonitorState("未恢复");
        List<SysMonitor> monitors = sysMonitorDao.queryAllByLimit(sysMonitor);
        return monitors;
    }

    @Override
    public Map<String, Object> getCountYearOrMonthOrDay(String startTime, String endTime) {
        Map<String,Object> result = new HashMap<>();
        // 历史报警统计
        List<Map<String, String>> gas = sysMonitorDao.getCountYearOrMonthOrDay("历史报警",startTime,endTime);
        int count = (int)sysMonitorDao.count(new SysMonitor());
        result.put("total",count);
        result.put("monitorType",gas);
        return result;
    }



}
